(function () {
    var serverData = {};
    var serverTable;
    var zdType;
    var Contract;
    var cOrderUnit;
    var cOrderArea;
    var industryInfo;
    var totalSum;
    var state;
    var contractType;
	var schedulePayDateStartDate;
	var schedulePayDateEndDate;
    var urlsum = window.rootPath+ '/ui/accountsreceivable/getSum?type=1';
    
    jQuery(document).ready(function () {
    	
         initQueryParam();
    	
    	$("#orderArea").multiselect({
    		header: true,
    		height: 175,
    		minWidth: 150,
    		classes: '',
    		checkAllText: '选中全部',
    		uncheckAllText: '取消全选',
    		noneSelectedText: '请勾选',
    		selectedText: '# 选中',
    		selectedList: 5,
    		show: null,
    		hide: null,
    		autoOpen: false,
    		multiple: true,
    		position: {},
    		appendTo: "body",
    		menuWidth:null
    	});
    	
    	$("#totalSum").multiselect({
		      header: true,
		      height: 250,
		      minWidth: 150,
		      classes: '',
		      checkAllText: '选中全部',
		      uncheckAllText: '取消全选',
		      noneSelectedText: '请勾选',
		      selectedText: '# 选中',
		      selectedList: 5,
		      show: null,
		      hide: null,
		      autoOpen: false,
		      multiple: true,
		      position: {},
		      appendTo: "body",
		      menuWidth:null
		    });
    	
    	$("#industryInfo").multiselect({
		      header: true,
		      height: 250,
		      minWidth: 150,
		      classes: '',
		      checkAllText: '选中全部',
		      uncheckAllText: '取消全选',
		      noneSelectedText: '请勾选',
		      selectedText: '# 选中',
		      selectedList: 5,
		      show: null,
		      hide: null,
		      autoOpen: false,
		      multiple: true,
		      position: {},
		      appendTo: "body",
		      menuWidth:null
		    });
    	
    	$("#contractState").multiselect({
    		header: true,
    		height: 250,
    		minWidth: 150,
    		classes: '',
    		checkAllText: '选中全部',
    		uncheckAllText: '取消全选',
    		noneSelectedText: '请勾选',
    		selectedText: '# 选中',
    		selectedList: 5,
    		show: null,
    		hide: null,
    		autoOpen: false,
    		multiple: true,
    		position: {},
    		appendTo: "body",
    		menuWidth:null
    	});
    	
    	$("#contractType").multiselect({
    		header: true,
    		height: 250,
    		minWidth: 150,
    		classes: '',
    		checkAllText: '选中全部',
    		uncheckAllText: '取消全选',
    		noneSelectedText: '请勾选',
    		selectedText: '# 选中',
    		selectedList: 5,
    		show: null,
    		hide: null,
    		autoOpen: false,
    		multiple: true,
    		position: {},
    		appendTo: "body",
    		menuWidth:null
    	});
    	
    	if(jQuery("#zdType").val()) {
    		serverData.zdType=jQuery("#zdType").val();
    		zdType = jQuery("#zdType").val();
    		urlsum += "&zdType=" + zdType;
    	}
    	if(jQuery("#hk").val()) {
    		serverData.hk=jQuery("#hk").val();
    		hk = jQuery("#hk").val();
    		urlsum += "&hk=" + hk;
    	}
    	if(jQuery("#orderArea").val()) {
    		serverData.cOrderArea=jQuery("#orderArea").val().toString();
    		cOrderArea = jQuery("#orderArea").val().toString();
    		urlsum += "&cOrderArea=" + cOrderArea;
    	}
    	if(jQuery("#industryInfo").val()) {
    		serverData.industryInfo=jQuery("#industryInfo").val().toString();
    		industryInfo=jQuery("#industryInfo").val().toString();
    		urlsum += "&industryInfo=" + industryInfo;
    	}
    	if(jQuery("#totalSum").val()) {
    		serverData.totalSum=jQuery("#totalSum").val().toString();
    		totalSum=jQuery("#totalSum").val().toString();
    		urlsum += "&totalSum=" + totalSum;
    	}
    	if(jQuery("#contractState").val()) {
    		serverData.state=jQuery("#contractState").val().toString();
    		state=jQuery("#contractState").val().toString();
    		urlsum += "&state=" + state;
    	}
    	if(jQuery("#contractType").val()) {
    		serverData.contractType=jQuery("#contractType").val().toString();
    		contractType=jQuery("#contractType").val().toString();
    		urlsum += "&contractType=" + contractType;
    	}
		if(jQuery("#startDate").val()) {
			serverData.schedulePayDateStartDate=jQuery("#startDate").val();
			schedulePayDateStartDate=jQuery("#startDate").val();
			urlsum += "&schedulePayDateStartDate=" + schedulePayDateStartDate;
		}
		if(jQuery("#endDate").val()) {
			serverData.schedulePayDateEndDate=jQuery("#endDate").val();
			schedulePayDateEndDate=jQuery("#endDate").val();
			urlsum += "&schedulePayDateEndDate=" + schedulePayDateEndDate;
		}
		getSum(urlsum);
        serverTable = jQuery('#dataTable').DataTable({
            "serverSide" : true,
            "ordering" : false,
            "ajax" : {
                "dataType" : "json",
                "type" : "post",
                "cache" : false,
                "url" : window.rootPath
                        + '/ui/accountsreceivable/getpage',
                "data" : function(d) {
                    return $.extend({}, d,
                            serverData);
                }
            },
            "columns" : [{
                            data : null,
                            defaultContent : ""// 排序   1
                        },
                        {
                            data : 'cOrderUnit',
    		                render:function(data,type,full,meta){
    		                	if(data == null || data == undefined || data == ""){
    								return data;
    							}else{
    								return (data.length > 5) ? '<font title="'+data+'">'+data.substring(0,5)+'...'+'</font>' : data;
    							}
    						}
                        },
                        {
                        	data : 'state'
                        },
                        {
                            data : 'nTotalSum'//合同总金额 3
                        },
                        {
                            data : 'cProjectName',
    		                render:function(data,type,full,meta){
    		                	if(data == null || data == undefined || data == ""){
    								return data;
    							}else{
    								return (data.length > 5) ? '<font title="'+data+'">'+data.substring(0,5)+'...'+'</font>' : data;
    							}
    						}
                        },
                        {
                        	data : 'contractType'//合同类型 4
                        },
                        {
                        	data : 'industryInfo'//合同类型 4
                        },
                        {
                            data : 'cSupplierCode',
    		                render:function(data,type,full,meta){
    		                	if(data == null || data == undefined || data == ""){
    								return data;
    							}else{
    								return (data.length > 5) ? '<font title="'+data+'">'+data.substring(0,5)+'...'+'</font>' : data;
    							}
    						}
                        },
                        {
                            data : 'cDemanderCode',
    		                render:function(data,type,full,meta){
    		                	if(data == null || data == undefined || data == ""){
    								return data;
    							}else{
    								return (data.length > 5) ? '<font title="'+data+'">'+data.substring(0,5)+'...'+'</font>' : data;
    							}
    						}
                        },
                        {
                            data : 'cProductKind',
    		                render:function(data,type,full,meta){
    		                	if(data == null || data == undefined || data == ""){
    								return data;
    							}else{
    								return (data.length > 5) ? '<font title="'+data+'">'+data.substring(0,5)+'...'+'</font>' : data;
    							}
    						}
                        },
                        {
                            data : 'cCheckKind',
    		                render:function(data,type,full,meta){
    		                	if(data == null || data == undefined || data == ""){
    								return data;
    							}else{
    								return (data.length > 5) ? '<font title="'+data+'">'+data.substring(0,5)+'...'+'</font>' : data;
    							}
    						}
                        },
                        {
                            data : 'cDutyOfficer'//责任人 9
                        },
                        {
                            data : 'cOrderArea'//片区 10
                        },
                        {
                            data : 'cPayType',
    		                render:function(data,type,full,meta){
    		                	if(data == null || data == undefined || data == ""){
    								return data;
    							}else{
    								return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
    							}
    						}
                        },
                        {
                            data : 'dSignedDate',//签订日期 12
                            render: function (data, type, full, meta) {
                                if (data)
                                    return moment(data).format('YYYY/MM/DD HH:mm');
                                else
                                    return '';
                            }
                        },
                        {
                            data : 'dDeliveryDate',//交货日期 13
                            render: function (data, type, full, meta) {
                                if (data)
                                    return moment(data).format('YYYY/MM/DD HH:mm');
                                else
                                    return '';
                            }
                        },
                        {
                            data : 'dLateestPayCheckDate',//最近开票日期 14
                            render: function (data, type, full, meta) {
                                if (data)
                                    return moment(data).format('YYYY/MM/DD HH:mm');
                                else
                                    return '';
                            }
                        },
                        {
                            data : 'nWQPWDQYSSum'//[未启票未到期应收] 15
                        },
                        {
                            data : 'nYQPWDQYSSum'//[已启票未到期应收] 16
                        },
                        {
                            data : 'nWDQYSYFKSum'//[未到期应收预付] 17
                        },
                        {
                            data : 'nWDQYSJDKSum'//[未到期应收进度] 18
                        },
                        {
                            data : 'nWDQYSFHKSum'//[未到期应收发货] 19
                        },
                        {
                            data : 'nWDQYSYSKSum'//[未到期应收验收] 20
                        },
                        {
                            data : 'nWDQYSTSKSum'//[未到期应收调试] 21
                        },
                        {
                            data : 'nWDQYSZBKSum'//[未到期应收质保] 22
                        },
                        {
                            data : 'nWDQYSYFSum'//[未到期应收运费] 23
                        },
                        {
                            data : 'nNowMonthYSYFKSum'//[本月应收预付] 24
                        },
                        {
                            data : 'nNowMonthYSJDKSum'//[本月应收进度] 25
                        },
                        {
                            data : 'nNowMonthYSFHKSum'//[本月应收发货] 26
                        },
                        {
                            data : 'nNowMonthYSYSKSum'//[本月应收验收] 27
                        },
                        {
                            data : 'nNowMonthYSTSKSum'//[本月应收调试] 28
                        },
                        {
                            data : 'nNowMonthYSZBKSum'//[本月应收质保] 29
                        },
                        {
                            data : 'nNowMonthYSYFSum'//[本月应收运费] 30
                        },
                        {
                            data : 'nBeforeYSYFKSum'//[前期应收预付] 31
                        },
                        {
                            data : 'nBeforeYSJDKSum'//[前期应收进度] 32
                        },
                        {
                            data : 'nBeforeYSFHKSum'//[前期应收发货] 33
                        },
                        {
                            data : 'nBeforeYSYSKSum'//[前期应收验收] 34
                        },
                        {
                            data : 'nBeforeYSTSKSum'//[前期应收调试] 35
                        },
                        {
                            data : 'nBeforeYSZBKSum'//[前期应收质保] 36
                        },
                        {
                            data : 'nBeforeYSYFSum'//[前期应收运费] 37
                        },
                        {
                            data : 'nNowMonthPayCheckSum'//[本期启票] 38
                        },
                        {
                            data : 'nEndingBalanceSum'//[期末余额] 39
                        },
                        {
                            data : 'nWQPContractEndingBalanceSum'//[未启票合同期末可收] 40
                        },
                        {
                            data : 'nYSYHKSum'//[已收预收货] 41
                        },
                        {
                            data : 'nNowMonthGetPaySum'//[本月回款额] 42
                        },
                        {
                            data : 'nWFHSKSum'//[无法收回款项] 43
                        },
                        {
                            data : 'nBeforeYearWDQYSKSum'//[上年未到期应收款] 44
                        },
                        {
                            data : 'nNowMonthContractAddSum'//[当月新增合同额] 45
                        },
                        {
                            data : 'nTax'//[税金] 46
                        },
                        {
                            data : 'nBookBalanceSum'//[账面余额] 47
                        }],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }
            }
        });
        serverTable.on('dblclick','tr',function(event) {
        	event = event|| window.event;
        	var target = event.target|| event.scrElement;
        	var data = serverTable.row(this).data();
        	var id=data.cContractId;
    		   	var type=data.contractType;
    		   	var url=window.rootPath+'/ui/payment/registPaySee/' + id +'/'+ type;
    		   	window.location.href = url;
        	return;
        });
    });
    
    //查询
    jQuery('#searchBtn').on('click',function(){
    	clearQueryServerData();
    	getServerDate();
        //serverData.type=jQuery('#type').val();
        serverTable.draw();
        getSum(urlsum)
    })
    
    
    var getServerDate=function(){
    	if(jQuery("#zdType").val()) {
    		serverData.zdType=jQuery("#zdType").val();
    		zdType = jQuery("#zdType").val();
    		urlsum += "&zdType=" + zdType;
    	}
    	if(jQuery("#hk").val()) {
    		serverData.hk=jQuery("#hk").val();
    		hk = jQuery("#hk").val();
    		urlsum += "&hk=" + hk;
    	}
    	if(jQuery("#Contract").val()){
    		serverData.Contract=jQuery("#Contract").val();
    		Contract = jQuery("#Contract").val().toString();
    		urlsum += "&Contract=" + Contract;
    	}
    	if(jQuery("#cOrderUnit").val()){
    		serverData.cOrderUnit=jQuery("#cOrderUnit").val();
    		cOrderUnit = jQuery("#cOrderUnit").val().toString();
    		urlsum += "&cOrderUnit=" + cOrderUnit;
    	}
    	if(jQuery("#orderArea").val()) {
    		serverData.cOrderArea=jQuery("#orderArea").val().toString();
    		cOrderArea = jQuery("#orderArea").val().toString();
    		urlsum += "&cOrderArea=" + cOrderArea;
    	}
    	if(jQuery("#industryInfo").val()) {
    		serverData.industryInfo=jQuery("#industryInfo").val().toString();
    		industryInfo=jQuery("#industryInfo").val().toString();
    		urlsum += "&industryInfo=" + industryInfo;
    	}
    	if(jQuery("#totalSum").val()) {
    		serverData.totalSum=jQuery("#totalSum").val().toString();
    		totalSum=jQuery("#totalSum").val().toString();
    		urlsum += "&totalSum=" + totalSum;
    	}
    	if(jQuery("#contractState").val()) {
    		serverData.state=jQuery("#contractState").val().toString();
    		state=jQuery("#contractState").val().toString();
    		urlsum += "&state=" + state;
    	}
    	if(jQuery("#contractType").val()) {
    		serverData.contractType=jQuery("#contractType").val().toString();
    		contractType=jQuery("#contractType").val().toString();
    		urlsum += "&contractType=" + contractType;
    	}
		if(jQuery("#startDate").val()) {
			serverData.schedulePayDateStartDate=jQuery("#startDate").val();
			schedulePayDateStartDate=jQuery("#startDate").val();
			urlsum += "&schedulePayDateStartDate=" + schedulePayDateStartDate;
		}
		if(jQuery("#endDate").val()) {
			serverData.schedulePayDateEndDate=jQuery("#endDate").val();
			schedulePayDateEndDate=jQuery("#endDate").val();
			urlsum += "&schedulePayDateEndDate=" + schedulePayDateEndDate;
		}
    	
    };
    
    var clearQueryServerData=function(option){
    	
    	if(serverData.zdType) {
    		serverData.zdType=null;
    		zdType=null;
    	}
    	if(serverData.hk) {
    		serverData.hk=null;
    		hk=null;
    	}
    	if(serverData.Contract) {
    		serverData.Contract=null;
    		Contract=null;
    	}
    	if(serverData.cOrderUnit) {
    		serverData.cOrderUnit=null;
    		cOrderUnit=null;
    	}
    	if(serverData.cOrderArea) {
    		serverData.cOrderArea=null;
    		cOrderArea=null;
    	}
    	if(serverData.industryInfo) {
    		serverData.industryInfo=null;
    		industryInfo=null;
    	}
    	if(serverData.totalSum) {
    		serverData.totalSum=null;
    		totalSum=null;
    	}
    	if(serverData.state) {
    		serverData.state=null;
    		state=null;
    	}
    	if(serverData.contractType) {
    		serverData.contractType=null;
    		contractType=null;
    	}
    	
    	if(serverData.schedulePayDateStartDate) {
    		serverData.schedulePayDateStartDate=null;
    		schedulePayDateStartDate=null;
    	}
    	if(serverData.schedulePayDateEndDate) {
    		serverData.schedulePayDateEndDate=null;
    		schedulePayDateEndDate=null;
    	}
    	
    	urlsum = window.rootPath+ '/ui/accountsreceivable/getSum?type=1';
    	
    }; 
    
    
    
})();

function getSum(urlsum){
	var urlsum1 = urlsum;
	var urlsum2 = urlsum;
	urlsum1 += '&sum=应收账金额'
	$.ajax({
        type: "GET", 
        dataType: 'json',
        async: false, 
        url: encodeURI(urlsum1),
        success: function(data) {
        	jQuery('#thisYearChanceCount').val(data.data);
        }
	});
	urlsum2 += '&sum=回款金额'
	$.ajax({
		type: "GET", 
		dataType: 'json',
		async: false, 
		url: encodeURI(urlsum2),
		success: function(data) {
			jQuery('#thisYearChanceSuccessCount').val(data.data);
		}
	});
}

//初始化查询条件
function initQueryParam(){
	if($('#endDate').val()== "") 
	$('#endDate').val(new Date().getFullYear()+"-"+((new Date().getMonth() + 1+"").length == 1 ? ("0"+(new Date().getMonth() + 1)) : new Date().getMonth() + 1) +"-"+new Date().getDate());
	if($('#startDate').val() == "")
	$('#startDate').val(new Date().getFullYear()+"-01-01");
}